package javafx.media;

import java.io.File;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;

public class MediaPlayerDemo extends Application {

	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		File file = new File(MediaPlayerDemo.class.getResource("/").getFile() + "/resources/b.mp4");
		Media media = new Media(file.toURI().toString());
		MediaPlayer mediaPlayer = new MediaPlayer(media);
		MediaView mediaView = new MediaView(mediaPlayer);
		mediaView.setFitHeight(800);
		mediaView.setFitWidth(450);
		mediaView.setX(0);
		mediaView.setY(0);
		
		stage.setTitle("MediaPlayer");

		BorderPane root = new BorderPane();
		root.setCenter(mediaView);

		Scene scene = new Scene(root, 800, 450);
		
		mediaView.setFitHeight(stage.getHeight());
		mediaView.setFitWidth(stage.getWidth());

		stage.heightProperty().addListener(new ChangeListener() {
			@Override
			public void changed(ObservableValue observable, Object oldValue, Object newValue) {
				mediaView.setFitHeight((Double) newValue);
			}

		});

		stage.widthProperty().addListener(new ChangeListener() {
			@Override
			public void changed(ObservableValue observable, Object oldValue, Object newValue) {
				mediaView.setFitWidth((Double) newValue);
			}
		});

		stage.setScene(scene);
		stage.show();

		mediaPlayer.play();
		// mediaPlayer.setAutoPlay(true);
	}

}
